﻿using System;
using System.Collections.Generic;
using System.Text;
using MigratorCore.Actions;
using MigratorCore.Bases;
using MigratorCore.Status;
using MigratorCore.Objects;
using MigratorCore;
using System.Data.OleDb;

namespace Migrator.WebWizForums.version7_9
{
    public class UserSource: ISourceAction
    {
        #region ISourceAction Members

        public void Get()
        {
            objects = new ObjectSet();
            status.AddEvent(new Event(EventType.Information, "Intilizing", "Intilizing Database Connection", DateTime.Now));
            OleDbConnection con = new OleDbConnection(settings["ConnectionString"].ToString());
            OleDbCommand cmd = new OleDbCommand("SELECT Count(Author_ID) FROM tblAuthor;", con);
            OleDbDataReader read;
            int c = 0;
            try
            {
                con.Open();
                c = Convert.ToInt32(cmd.ExecuteScalar());
                cmd = new OleDbCommand("SELECT Username, Real_name, Author_email, Show_email, Homepage, Location, MSN, Yahoo, ICQ, AIM, Occupation, Interests, DOB, Signature, Join_date, Last_visit, Time_offset_hours FROM tblAuthor;", con);
                read = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            }
            catch (Exception e)
            {
                status.Completed = 100;
                status.AddEvent(new Event(EventType.CriticalError, "Data Connection Failed!", e.Message, e, DateTime.Now));
                return;
            }
            status.AddEvent(new Event(EventType.Information, "Loaded " + c.ToString() + " users", "Loaded " + c.ToString() + " users from Source Database", DateTime.Now));
            int x = 0;
            while (read.Read())
            {
                x++;
                if (!read.IsDBNull(2) && !read.GetString(0).Equals("Guests"))
                {
                    User user = new User(read.GetString(0), read.GetString(2).Replace("mailto:", ""));
                    if (!read.IsDBNull(1)) 
                        try {
                            user.CommonName = read.GetString(1);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Common Name data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(3))
                        try {
                            user.EnableEmail = read.GetBoolean(3);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing profile data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(4))
                        try {
                            user.WebAddress = read.GetString(4);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing web address data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(5))
                        try {
                            user.Location = read.GetString(5);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing location data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(6))
                        try {
                            user.Msn = read.GetString(6);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has Msn profile data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(7))
                        try {
                            user.Yahoo = read.GetString(7);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Yahoo data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(8))
                        try {
                            user.Icq = read.GetString(8);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Icq data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(9))
                        try {
                            user.Aol = read.GetString(9);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Aol data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(10))
                        try {
                            user.Occupation = read.GetString(10);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Occupation data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(11))
                        try {
                            user.Interests = read.GetString(11);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Interests data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(12))
                        try {
                            user.Birthday = read.GetDateTime(12);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Birthday data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(13))
                        try {
                            user.Signature = read.GetString(13);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Signature data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(14)) 
                        try {
                            user.Datejoined = read.GetDateTime(14);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Date Joined data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(15))
                        try {
                            user.Datelastvisited = read.GetDateTime(15);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Date Last Visited data", e, DateTime.Now));
                        }
                    if (!read.IsDBNull(16))
                        try {
                            user.Timezone = read.GetInt32(16);
                        }
                        catch (Exception e)
                        {
                            status.AddEvent(new Event(EventType.MissingData, "Missing Profile Data", read.GetString(0) + " has missing Timezone data", e, DateTime.Now));
                        }
                    Objects.Add(user);
                    status.Completed = Convert.ToInt32(Math.Round((double)((c / x) * 100)));
                    status.AddEvent(new Event(EventType.Information, "Processed user " + read.GetString(0), "Processed user " + read.GetString(0), DateTime.Now));
                }
            }
        }

        #endregion

        #region IAction Members

        private Settings settings;
        public Settings Settings
        {
            get
            {
                if (settings == null)
                {
                    settings = new Settings();
                    settings.Add("ConnectionString", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Databases\\www.mdb;User Id=admin;Password=;");
                }
                return settings;
            }
        }

        public string Name
        {
            get { return "Users Source"; }
        }

        private Status status;
        public Status Status
        {
            get
            {
                if (status == null)
                    status = new Status();
                return status;
            }
        }

        private ObjectSet objects;
        public ObjectSet Objects
        {
            get
            {
                if (this.objects == null) this.objects = new ObjectSet();
                return this.objects;
            }
            set
            {
                this.objects = value;
            }
        }

        public ActionType ActionType
        {
            get
            {
                return ActionType.Users;
            }
        }

        #endregion
    }
}
